package cn.aitrox.ry.service.anno.controller;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.common.controller.BaseController;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.anno.api.ComponentResultApi;
import cn.aitrox.ry.service.anno.dto.anno.component.result.AcrDto;
import cn.aitrox.ry.service.anno.dto.anno.component.result.in.AcrCreateInDto;
import cn.aitrox.ry.service.anno.dto.anno.component.result.in.AcrUpdateInDto;
import cn.aitrox.ry.service.anno.dto.anno.component.result.out.AcrCreateOutDto;
import cn.aitrox.ry.service.anno.service.AnnoComponentResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ComponentResultController extends BaseController implements ComponentResultApi {

    @Autowired
    private AnnoComponentResultService componentResultService;

    @PostMapping(value = "/anno/component/result")
    @Override
    public RespResult<AcrCreateOutDto> create(@Validated @RequestBody AcrCreateInDto input) {
        // 校验是否有重复的组件
        long count = input.getAcrList().stream().map(AcrDto::getComponentId).distinct().count();
        if (count != input.getAcrList().size()) {
            RespResult.secByError(RespCodeEnum.PARAM_ERROR.getCode(), "存在重复征象");
        }
        return componentResultService.batchCreate(input.getArId(), input.getAlrId(), input.getAcrList());
    }

    @PutMapping(value = "/anno/component/result")
    @Override
    public RespResult update(@Validated @RequestBody AcrUpdateInDto input) {
        return componentResultService.update(input.getAcrId(), input.getResult());
    }
}
